********************************************************************************
** 	TITLE:		CA00_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       cand                                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "CA00_results.smcl", replace

* Download the "cand.txt" file from this link: https://www.elections.ca/content.aspx?section=res&document=candexe&dir=rep/off/37p&lang=e

* Import the dataset

import delimited cand.txt, delimiters(tab) clear

* Keep 2000 election results

keep if event_number == 3700

* Generate party variable

gen party = .

replace party = 1 if candidate_party_english_name == "Liberal"
replace party = 2 if candidate_party_english_name == "Bloc Québécois"
replace party = 3 if candidate_party_english_name == "Reform" | candidate_party_english_name == "Canadian Alliance"
replace party = 4 if candidate_party_english_name == "Progressive Conservative"
replace party = 5 if candidate_party_english_name == "N.D.P."
replace party = 97 if party == .

* Generate rank of parties

gsort ed_code -candidate_vote
bysort ed_code : gen rank00 = _n

gen first00 = party if rank00 == 1
gen second00 = party if rank00 == 2
gen third00 = party if rank00 == 3

bysort ed_code : gen winner00 = first00[1]
bysort ed_code : gen runnerup00 = second00[2]
bysort ed_code : gen thirdplace00 = third00[3]

* Generate margins of victory

bysort ed_code : gen margin00 = candidate_vote_percentage[1] - candidate_vote_percentage[2]

* Generate effective number of electoral parties

gen sqrvotepr00 = (candidate_vote_percentage/100)^2

bysort ed_code : egen sumsqrvotepr00 = sum(sqrvotepr00)

gen enep00 = 1/sumsqrvotepr00

* Labelling parties

label define party 1 "Liberal Party" 2 "Bloc Quebecois" 3 "Reform/Canadian Alliance" 4 "Progressive Conservative" 5 "NDP" 97 "Other"
label values winner00 runnerup00 thirdplace00 party

* Generate district numbers

replace ed_code = subinstr(ed_code, "-", "",.) 
replace ed_code = subinstr(ed_code, "ED", "",.)

* Selecting, ordering, and sorting variables

keep ed_code party rank00 candidate_vote candidate_vote_percentage margin00 enep00 winner00 runnerup00 thirdplace00

order ed_code party rank00 candidate_vote candidate_vote_percentage margin00 enep00 winner00 runnerup00 thirdplace00

sort ed_code rank00

drop if rank00 > 3

* Drop duplicates

duplicates tag ed_code, gen(dup)
duplicates drop ed_code, force
drop dup

* Saving file

save CA00_results, replace

* Drop duplicates

duplicates tag ed_code, gen(dup)
duplicates drop ed_code, force
drop dup

* Merge with CA97_results.dta and generate reelection variable

merge 1:1 _n using CA97_results.dta

* Generate variable for incumbent party reelection

gen reelected00 = 1 if winner00 == winner97
replace reelected00 = 0 if reelected00!=1

* Create election variable

gen election00 = "2000 Canadian federal election"

* Saving file

save CA9700_results, replace

log close